package tclp.analysis;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Absatz Leoš
 */
public class CombatLogAnalysis {

  protected File combatLogFile;

  public CombatLogAnalysis(String path) {
    this.combatLogFile = new File(path);
  }

  public AnalysisResult analyse() throws FileNotFoundException, IOException, ParseException {
    AnalysisResult result = new AnalysisResult();
    BufferedReader reader = new BufferedReader(new FileReader(combatLogFile));
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS");

    long damageDone = 0;
    String line = reader.readLine();
    String[] parts = line.split("-");
    String time = parts[0];
    String damage = null;
    Date startDate = dateFormat.parse(time);
    while (line != null) {
      parts = line.split("-");
      time = parts[0].trim();
      damage = parts[1].trim();
      
      if ((damage.startsWith("Hit")) || (damage.startsWith("Cri"))) {
        // @todo differentiate hits and crits
        long damageNum = Long.parseLong(damage.substring(3));
        damageDone += damageNum;
      } else {
        // @todo damage taken
      }

      line = reader.readLine();
    }
    Date endTime = dateFormat.parse(time);
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startDate);
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(endTime);
    long timeDiff = endCal.getTimeInMillis() - startCal.getTimeInMillis();
     
    result.setDamageDone(damageDone);
    result.setTimeElapsed(timeDiff);

    reader.close();
    return result;
  }
}
